package com.yksj.healthtalk.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.universalimageloader.core.DisplayImageOptions;
import org.universalimageloader.core.ImageLoader;
import org.universalimageloader.core.display.RoundedBitmapDisplayer;
import org.universalimageloader.utils.StorageUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.MyDoctorListEntity;
import com.yksj.healthtalk.entity.UrlEntity;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;

public class DoctorExpandableListAdapter extends BaseExpandableListAdapter {

	private Context context;
	public List<MyDoctorListEntity> entities =new ArrayList<MyDoctorListEntity>();
	private LayoutInflater inflater;
	private ViewHolder holder;
	public DoctorExpandableListAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
			MyDoctorListEntity entity;
			if (groupPosition == 0 && childPosition ==0) {
				entity = entities.get(0);
			}else {
				entity = entities.get(childPosition+1);
			}
			if(convertView==null){
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.nine_image_list_item, null);
				holder.mHeader = (GridView) convertView.findViewById(R.id.head_image);
				holder.mName = (TextView) convertView.findViewById(R.id.name);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			
			if(groupPosition == 0 && childPosition ==0){
//				holder.mName.setText(entity.getName());
				holder.mName.setText(entity.getName()+"("+entity.getmImageEntity().getNum()+")");
			}else
			 if(groupPosition == 1 &&childPosition!=2){
				if(entity.getmImageEntity().getNum()==-1){//不显示num
					holder.mName.setText(entity.getName());
				}else{
					holder.mName.setText(entity.getName()+"("+entity.getmImageEntity().getNum()+")");
				}
			}
			
			if(childPosition==2){
				holder.mHeader.setVisibility(View.INVISIBLE);
				holder.mName.setText(entity.getName());
			}else{
				holder.mHeader.setVisibility(View.VISIBLE);
				holder.mHeader.setAdapter(new myGrived(entity.getHeaderPath()));
//				loader.displayImage(FriendUtil.getHeaderPath(entity.getHeaderPath().getList()), holder.mHeader, mUnknowDisplayImageOptions);
			}
			
		return convertView;
	}

	
	class myGrived extends BaseAdapter{
		private  List<UrlEntity> urlEntities=new ArrayList<UrlEntity>();
		private ImageLoader instance;
		public myGrived(List<UrlEntity> urlEntities) {
			instance = ImageLoader.getInstance();
			this.urlEntities.clear();
			if(urlEntities!=null)
			this.urlEntities.addAll(urlEntities);
		}

		@Override
		public int getCount() {
			return urlEntities.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}
		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView=new ImageView(context);
			imageView.setLayoutParams(new AbsListView.LayoutParams(33,33));
			instance.displayImage(urlEntities.get(position).getSex(), urlEntities.get(position).getUrl(), imageView);
			return imageView;
		}
		
	}
	class ViewHolder {
		GridView mHeader;
		TextView mName;
	}
	
	@Override
	public int getChildrenCount(int groupPosition) {
		if(entities.size()==0)return 0;
		if(groupPosition==0) return 1;
		return 3;
	}

	public void onBound(List<MyDoctorListEntity> datas){
		entities.clear();
		entities.addAll(datas);
		notifyDataSetChanged();
	}
	
	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 如果是医生的话 只有一个 我的医生列表
	 */
	@Override
	public int getGroupCount() {
		if(entities.size()==0)return 0;
		if(SmartFoxClient.getLoginUserInfo().isDoctor())return 1;
		return 2;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		convertView = inflater.inflate(R.layout.knowledge_detail_expand_group,null);
		TextView name = (TextView) convertView.findViewById(R.id.expand_group_text);
		ImageView imageView = (ImageView) convertView.findViewById(R.id.expand_group_head);
		name.setText("服务医生");

		if (groupPosition == 0) {
			TextView textView = new TextView(context);
			textView.setHeight(0);
			return textView;

		} else {
			if (isExpanded) {
				imageView.setBackgroundResource(R.drawable.friend_group_arrows_down);
			} else {
				imageView.setBackgroundResource(R.drawable.friend_group_arrows);
			}
			return convertView;
		}
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

}
